﻿using SQLiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils.Common;
using ZK.Entity;

namespace ZK.BLL
{
    public class CodeRuleBLL
    {

        private CodeRuleBLL() { }
        /// <summary>
        /// 窗体对象实例
        /// </summary>
        private static CodeRuleBLL _instance;
        public static CodeRuleBLL Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CodeRuleBLL();
                return _instance;
            }
        }


        public string GetNewID()
        {
            return BaseIdGenerator.Instance.GetId();
        }



        public List<CodeRule> GetList()
        {
            string sql = "Select * From CodeRule Order BY Id ";
            return SQLiteORM.Instance.GetEntityList<CodeRule>(sql).ToList();
        }



        private bool DeleteAll( )
        {
            string sql = " Delete from  CodeRule ";
            return SQLiteORM.Instance.Delete(sql);
        }


        public bool InsertList(List<CodeRule> list)
        {
            DeleteAll();
            return SQLiteORM.Instance.InsertListByTransaction<CodeRule>(list);
        }




        public string BuildCode()
        {


            try
            {
                List<CodeRule> list = GetList();
                StringBuilder sb = new StringBuilder();
                foreach (CodeRule codeRule in list)
                {
                    switch ( codeRule.CodeRuleType)
                    {
                        case CodeRuleType.固定值:
                            {
                                sb.Append(CodeRuleLen(codeRule.DefaultValue, codeRule.CodeRuleLen));
                                break;
                            };
                     
                        case CodeRuleType.流水号:
                            {
                                sb.Append(CodeRuleLen(SeqenceBLL.Instance.GetSerialNumber(codeRule.DefaultValue), codeRule.CodeRuleLen));
                                break;
                            }
                        case CodeRuleType.时间戳:
                            {
                                sb.Append(CodeRuleLen(TimeStamp(), codeRule.CodeRuleLen));
                                break;
                            }
                        case CodeRuleType.随机数:
                            {
                                sb.Append(GetRandNumber(codeRule.CodeRuleLen));
                                break;
                            }
                        case CodeRuleType.当前日期:
                            {
                                sb.Append(CodeRuleLen(DateTime.Now.ToString(codeRule.DefaultValue), codeRule.CodeRuleLen));
                                break;
                            }
                        case CodeRuleType.按日流水:
                            {
                                sb.Append(CodeRuleLen(SeqenceBLL.Instance.GetSerialNumber("按日流水" + DateTime.Now.ToString("yyyyMMdd")  ), codeRule.CodeRuleLen));
                                break;
                            }
                        case CodeRuleType.按月流水:
                            {
                                sb.Append(CodeRuleLen(SeqenceBLL.Instance.GetSerialNumber("按月流水" + DateTime.Now.ToString("yyyyMM") ), codeRule.CodeRuleLen));
                                break;
                            }
                    }
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.LogError(ex.Message, ex);
                return "";
            }
        }

        private string CodeRuleLen(object obj, int len)
        {
            string code = string.Empty;
            if (obj == null)
            {
                return code.PadLeft(len, '0');
            }
            else
            {
                code = obj.ToString();
            }

            if (code.Length > len)
            {
                return code.Substring(code.Length - len, len);

            }
            if (code.Length < len)
            {
                return code.PadLeft(len, '0');
            }
            else
            {
                return code;
            }
        }

        /// <summary>
        ///获取时间戳(13位) 
        /// </summary>
        /// <returns></returns>
        public static string TimeStamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0, 0);
            string timeStamp = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            return timeStamp;
        }

        /// <summary>
        /// 随机数
        /// </summary>
        public static String GetRandNumber(int totalWidth)
        {

            string strValue = string.Empty;

            for (var j = 0; j < totalWidth; j++)
            {
                var r = new Random(Guid.NewGuid().GetHashCode());
                strValue += r.Next(0, 10).ToString();
            }
            return strValue;
        }
    }
}
